<template>
  <div>
    <el-select v-model="type" clearable placeholder="请选择类别">
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      />
    </el-select>
    <el-input v-model="username" placeholder="请输入用户名" />
    <!-- 列表 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="用户名">
        <template #default="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column label="理由">
        <template #default="scope">
          {{ scope.row.reason }}
        </template>
      </el-table-column>
      <el-table-column label="开始时间">
        <template #default="scope">
          {{ dayjs(scope.row.satrt).format('YYYY年MM月DD日 HH:mm:ss') }}
        </template>
      </el-table-column>
      <el-table-column label="结束时间">
        <template #default="scope">
          {{ dayjs(scope.row.end).format('YYYY年MM月DD日 HH:mm:ss') }}
        </template>
      </el-table-column>
      <el-table-column label="附件">
        <template #default="scope">
          <el-image
            v-if="scope.row.extend"
            style="width: 100px; height: 100px"
            :src="'/api/' + scope.row.extend.split(',')[0]"
            :zoom-rate="1.2"
            :max-scale="7"
            :min-scale="0.2"
            preview-teleported="true"
            :preview-src-list="
              scope.row.extend.split(',').map((item: any) => {
                return '/api/' + item
              })
            "
            :initial-index="4"
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template #default="scope">
          <div type="warning" v-if="scope.row.type == 1">
            <el-button type="success" @click="update(scope.row, 2)">同意</el-button>
            <el-button type="danger" @click="update(scope.row, 3)">拒绝</el-button>
          </div>
          <el-button type="success" v-if="scope.row.type == 2">已同意</el-button>
          <el-button type="danger" v-if="scope.row.type == 3">已拒绝</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleYes(scope.$index, scope.row)">同意</el-button>
          <el-button size="small" type="danger" @click="handleNo(scope.$index, scope.row)"
            >拒绝</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="pageindex"
        v-model:page-size="pageNum"
        :page-sizes="[10, 30, 50, 100]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @change="change"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { developerQuery, developerAdd,developerGroup } from '../services/developerService'
import { reactive, ref, onMounted, watch } from 'vue'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'

const tableData = ref<Array<any>>([]) //列表渲染

interface Leaves {
  _id?: string
  type: number
}

// 分页
let pageindex = ref(1) //显示第几页
let pageNum = ref(2) //每页显示数据
let total = ref(100) //数据总数
const small = ref(false)
const background = ref(false)
const disabled = ref(false)

const change = (val1: number, val2: number) => {
  pageindex.value = val1
  search()
}

// 搜索
let username = ref('')
// 数据渲染
onMounted(async () => {
  search()
})

let search = async () => {
  let res = await developerQuery({
    type: type.value,
    pageindex: pageindex.value,
    pageNum: pageNum.value,
  })
  tableData.value = res.data.data
  total.value = res.data.total
}

// 同意和拒绝
let update = async (item: any, type: any) => {
  // let res = await leavesUpdate({ _id: item._id, type })
  // if (res.data.code == 200) {
  //   ElMessage(res.data.msg)
  //   item.type = type
  // } else {
  //   ElMessage(res.data.msg)
  // }
}

let type = ref()
const options = [
  {
    value: '1',
    label: '待审批'
  },
  {
    value: '2',
    label: '已同意'
  },
  {
    value: '3',
    label: '已拒绝'
  }
]

watch([type], () => {
  pageindex.value = 1
  search()
})
</script>

<style lang="scss">
.el-col-16 {
  min-width: 90% !important;
}
.cell {
  text-align: center;
}
.el-dialog {
  --el-dialog-width: 40% !important;
}
</style>
